//
//  HSCLog.m
//  SSC
//
//  Created by 红沙尘 on 17/6/7.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "HSCLog.h"
#import "SQLITE.h"

@implementation HSCLog

+ (BOOL)saveDebugWithURL:(NSString *)url
                   param:(NSDictionary *)param
                  result:(NSDictionary *)result {
    
    NSMutableDictionary *obj = @{}.mutableCopy;
    
    obj[@"url"]      = url;
    obj[@"apiparam"] = [self dataTOjsonString:param];
    obj[@"result"]   = [self dataTOjsonString:result];
    obj[@"date"]     = [self today];
    BOOL ok = [[SQLITE standardSqlite] addSQLObject:obj forKey:@"log"];
    return ok;
}

+ (NSString*)dataTOjsonString:(id)object
{
    if (!object) return @"";
    
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if (!jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

+ (NSString *)today {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    return [formatter stringFromDate:[NSDate date]];
}

@end
